package com.magic.gre.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.TextView;

import com.magic.gre.R;
import com.magic.gre.adapter.PagerAdapterHome;
import com.magic.gre.base.activity.BaseMVPActivity;
import com.magic.gre.mvp.contract.MainContract;
import com.magic.gre.mvp.presenter.MainPresenterImpl;
import com.magic.gre.ui.fragment.AvtivityFragment;
import com.magic.gre.ui.fragment.HomeFragment;
import com.magic.gre.ui.fragment.MineFragment;
import com.noname.lib_base_java.entity.MsgEvent;
import com.noname.lib_base_java.util.ActivityManager;
import com.noname.lib_base_java.util.L;
import com.noname.lib_base_java.widget.UnScrollViewPager;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

public class MainActivity extends BaseMVPActivity<MainPresenterImpl> implements MainContract.View {


    @BindView(R.id.mUnScrollViewPager)
    UnScrollViewPager mUnScrollViewPager;
    @BindView(R.id.tb1)
    TextView tb1;
    @BindView(R.id.tb2)
    TextView tb2;
    @BindView(R.id.tb3)
    TextView tb3;

    public static void startThis(Context context) {
        Intent intent = new Intent(context.getApplicationContext(), MainActivity.class);
        context.startActivity(intent);
    }

    private View[] tabViewArray;

    private ArrayList<Fragment> fragmentList = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected boolean isImmerse() {
        return true;
    }

    @Override
    protected MainPresenterImpl creatPresenter() {
        return new MainPresenterImpl(this);
    }


    @Override
    protected void netOverride() {

    }

    @Override
    protected void handlerMsg(MsgEvent msgEvent) {
        super.handlerMsg(msgEvent);
        switch (msgEvent.getEvent()) {
            case MsgEvent.SHOW_MODLE:
                int tag = (int) msgEvent.get("tag");
                setlectTab(tabViewArray[tag]);
                break;
        }
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        super.initView(savedInstanceState);

        registerRxBus();

        tabViewArray = new View[]{tb1, tb2, tb3};

        fragmentList.add(HomeFragment.newInstance());
        fragmentList.add(AvtivityFragment.newInstance());
        fragmentList.add(MineFragment.newInstance());

        mUnScrollViewPager.setAdapter(new PagerAdapterHome(getSupportFragmentManager(), fragmentList));
        mUnScrollViewPager.setOffscreenPageLimit(3);
        setlectTab(tb1);
    }

    @OnClick(value = {R.id.tb1, R.id.tb2, R.id.tb3})
    protected void onClick(View view) {
        switch (view.getId()) {
            case R.id.tb1:
            case R.id.tb2:
                setlectTab(view);
                break;
            case R.id.tb3:
                if (isValidAccount()) {
                    setlectTab(view);
                }
                break;
        }
    }

    @Override
    public void onCancel() {

    }

    @Override
    public void onDialogMiss() {

    }

    @Override
    public void onBackPressed() {
        ActivityManager.getInstance().exit();
        super.onBackPressed();
    }

    private void setlectTab(View view) {
        if (tabViewArray != null) {
            view.setSelected(true);
            for (int i = 0; i < tabViewArray.length; i++) {
                if (tabViewArray[i] != view) {
                    tabViewArray[i].setSelected(false);
                } else {
                    mUnScrollViewPager.setCurrentItem(i, false);
                }
            }
        }
    }
}
